package com.controller;

import com.dao.FAddressDao;
import com.dao.FCartDao;
import com.dao.FOderDetailDao;
import com.dao.FOrderDao;
import com.model.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class OrderController {
    @Resource
    FOrderDao orderDao;
    @Resource
    FOderDetailDao fOderDetailDao;
    @Resource
    FAddressDao fAddressDao;
    @Resource
    FCartDao fCartDao;
    //前台订单列表
    @GetMapping(value = "/userCenterOrder")
    public String userOrderList(ModelMap model, HttpServletRequest request){
        HttpSession session=request.getSession();
        FUser user=(FUser) session.getAttribute("USER_SESSION");
        Map<FOrder,List<FOrderDetail>> map=new LinkedHashMap<>();
        //定义字典key :商品分类名称，value:对应分类下的商品列表
        List<FOrder> orderList=orderDao.getOrderByUserId(user.getId());
        //遍历商品类型列表，读取对应商品类型的商品数据
        for (FOrder fOrder:orderList){
            List<FOrderDetail> orderDetailList=fOderDetailDao.getByOrderId(fOrder.getId());
            map.put(fOrder,orderDetailList);
        }
        model.addAttribute("list",map);
        model.addAttribute("user",user);
        return "front/user_center_order";
    }
    //后台购物车显示列表
    @GetMapping(value = "/orderList")
    public String list(ModelMap model, Page page) {
        //获取商品类型列表
        List<FOrder> list =orderDao.list(page);
        //遍历商品类型列表，读取对应商品类型的商品数据
        int total=orderDao.total(page);
        page.caculateLast(total);
        model.addAttribute("list", list);
        model.addAttribute("page", page);
        model.addAttribute("total",total);
        return "backend/order-list";
    }
    //购物车点击去结算
    @GetMapping(value = "/placeOrder")
    public String addOrderView(ModelMap model, HttpServletRequest request){
        HttpSession session=request.getSession();
        FUser user=(FUser) session.getAttribute("USER_SESSION");
        FAddress fAddress=fAddressDao.getAddressByUserId(user.getId());
        //定义字典key :商品分类名称，value:对应分类下的商品列表
        List<FCart> fCartList=fCartDao.getCartByUserId(user.getId());
        //遍历商品类型列表，读取对应商品类型的商品数据
        double total_price=0;
        for (FCart fc:fCartList){
            total_price+=fc.getbGood().getPrice()*fc.getNum();
        }
        int total_num=fCartList.stream().mapToInt(FCart::getNum).sum();

        model.addAttribute("fAddress",fAddress);
        model.addAttribute("fCartList",fCartList);
        model.addAttribute("total_price",String.format("%.2f",total_price));
        model.addAttribute("total_num",total_num);
        return "front/place_order";
    }
    //提交订单
    @PostMapping(value = "/addOrder")
    public String addOrder(ModelMap model, HttpServletRequest request,FOrder fOrder) {
        HttpSession session = request.getSession();
        FUser user = (FUser) session.getAttribute("USER_SESSION");
        //读取收货地址
        FAddress fAddress=fAddressDao.getAddressByUserId(user.getId());
        //如果用户没有设置收货地址，则跳转到收货地址设置页面
        if(fAddress==null){
            return "front/user_center_site";
        }
        //生成唯一订单号
        String order_no= UUID.randomUUID().toString().replace("-","").substring(12);
        fOrder.setOrder_no(order_no);
        fOrder.setUser_name(fAddress.getUser_name());
        fOrder.setAddress(fAddress.getAddress());
        //获取当前时间并转成字符串
        SimpleDateFormat tempDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=tempDate.format(new java.util.Date());
        fOrder.setTime(time);
        fOrder.setStatus(1);
        fOrder.setPhone(fAddress.getPhone());
        fOrder.setUser_id(user.getId());
        //读取购物车
        List<FCart> fCartList=fCartDao.getCartByUserId(user.getId());
        double total_price=0;
        for (FCart fc:fCartList){
            total_price+=fc.getbGood().getPrice()*fc.getNum();
        }
        fOrder.setTotal_price(total_price);
        int row=orderDao.insertOrder(fOrder);
        if(row<1){
            return "front/login";
        }
        for (FCart fCart:fCartList){
            FOrderDetail fOrderDetail=new FOrderDetail();
            fOrderDetail.setOrder_id(fOrder.getId());
            fOrderDetail.setImg_url(fCart.getbGood().getImg_url());
            fOrderDetail.setGood_name(fCart.getbGood().getGood_name());
            fOrderDetail.setNum(fCart.getNum());
            fOrderDetail.setUnit(fCart.getbGood().getUnit());
            fOrderDetail.setIntro(fCart.getbGood().getIntro());
            fOrderDetail.setPrice(fCart.getbGood().getPrice());
            fOrderDetail.setTotal_price(fCart.getbGood().getPrice()*fCart.getNum());
            fOderDetailDao.insert(fOrderDetail);
        }
        //添加订单成功，清空购物车
        fCartDao.cleanCart();
        model.addAttribute("fAddress",fAddress);
        model.addAttribute("fCartList",fCartList);
        model.addAttribute("user",user);
        return "redirect:userCenterOrder";//注意这里不是跳转到静态页面，是跳转到用户中心——订单列表控制器
    }

}
